package com.clearance.mine_clearance.untils;


import com.alibaba.fastjson.JSON;
import com.clearance.mine_clearance.bot.MineClearanceBot;
import com.clearance.mine_clearance.config.Constants;
import com.clearance.mine_clearance.entry.TgUser;
import com.clearance.mine_clearance.mapper.TgUserNapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.telegram.telegrambots.meta.api.methods.ParseMode;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
public class TgUserUntils {

    private static TgUserNapper tgUserNapper;

    static {
        tgUserNapper = (TgUserNapper) BeanUtils.getBean("tgUserNapper");
    }

    public static void initMessageUser(Update update) {
        User user;
        if (update.getCallbackQuery() != null) {
            user = update.getCallbackQuery().getFrom();
        } else {
            user = update.getMessage().getFrom();
            if (update.getMessage().getText().contains(Constants.start)) {
                return;
            }
        }
        TgUser selectByTgId = tgUserNapper.selectByTgId(user.getId().toString());
        if (selectByTgId == null) {
            TgUser tgUser = new TgUser();
            tgUser.setUserName(user.getUserName());
            tgUser.setTgId(user.getId().toString());
            tgUser.setIsBot(user.getIsBot() ? 0 : 1);
            String firstNam = "";
            if (StringUtils.isNotEmpty(user.getFirstName())) {
                firstNam = firstNam + user.getFirstName();
            }
            if (StringUtils.isNotEmpty(user.getLastName())) {
                firstNam = firstNam + user.getLastName();
            }
            tgUser.setFirstName(firstNam);
            tgUser.setUserName(user.getUserName());
            tgUser.setBalance(BigDecimal.ZERO);
            tgUser.setType(0);
            tgUser.setCreateTime(new Date());
            tgUser.setGivePayType(0);
            tgUser.setDarkLightning(1);
            tgUser.setFrozenAmount(BigDecimal.ZERO);
            tgUser.setChatInviteLinkUrl(InviteLinkUntils.getChatInviteIinkUrl(tgUser));
            tgUserNapper.insert(tgUser);
            RedisUntils.setValue(Constants.TGUSER + tgUser.getTgId(), JSON.toJSONString(tgUser));
            log.info("添加用户信息{}", tgUser);
        } else {
            updateUser(user,selectByTgId);
        }
    }

    public static void initMessage(Update update) {
        List<User> newChatMembers = update.getMessage().getNewChatMembers();
        if (newChatMembers != null && !newChatMembers.isEmpty()) {
            for (User user : newChatMembers) {
                SendMessage sendMessage = new SendMessage();
                sendMessage.setChatId(update.getMessage().getChatId());
                TgUser selectByTgId = tgUserNapper.selectByTgId(user.getId().toString());
                if (selectByTgId == null) {
                    TgUser tgUser = new TgUser();
                    tgUser.setUserName(user.getUserName());
                    tgUser.setTgId(user.getId().toString());
                    tgUser.setIsBot(user.getIsBot() ? 0 : 1);
                    String firstNam = "";
                    if (StringUtils.isNotEmpty(user.getFirstName())) {
                        firstNam = firstNam + user.getFirstName();
                    }
                    if (StringUtils.isNotEmpty(user.getLastName())) {
                        firstNam = firstNam + user.getLastName();
                    }
                    tgUser.setFirstName(firstNam);
                    tgUser.setBalance(BigDecimal.ZERO);
                    tgUser.setType(0);
                    tgUser.setCreateTime(new Date());
                    tgUser.setFrozenAmount(BigDecimal.ZERO);
                    tgUserNapper.insert(tgUser);
                    RedisUntils.setValue(Constants.TGUSER + tgUser.getTgId(), JSON.toJSONString(tgUser));
                    log.info("添加用户信息{}", tgUser);
                    //欢迎新成员;
                    sendMessage.setText("\uD83D\uDCE3\uD83D\uDCE3\uD83D\uDCE3欢迎老板<code>【" + user.getFirstName() + "】</code>进入本群\n"
                            + "❤\uFE0F\u200D\uD83D\uDD25❤\uFE0F\u200D\uD83D\uDD25祝您在本群旗开得胜\uD83D\uDEA9,财运连连\uD83D\uDCB8");
                } else {
                    sendMessage.setText("\uD83D\uDCE3\uD83D\uDCE3\uD83D\uDCE3欢迎老板<code>【" + user.getFirstName() + "】</code>重返本群\n"
                            + "❤\uFE0F\u200D\uD83D\uDD25❤\uFE0F\u200D\uD83D\uDD25祝您在本群旗开得胜\uD83D\uDEA9,财运连连\uD83D\uDCB8");
                }
                sendMessage.setParseMode(ParseMode.HTML);
                new MineClearanceBot().sendMessage(sendMessage);
            }
        }
    }

    public static void updateUser(User user,TgUser selectByTgId){
        String firstNam = "";
        if (StringUtils.isNotEmpty(user.getFirstName())) {
            firstNam = firstNam + user.getFirstName();
        }
        if (StringUtils.isNotEmpty(user.getLastName())) {
            firstNam = firstNam + user.getLastName();
        }
        if (StringUtils.isNotEmpty(user.getUserName())) {
            if (StringUtils.isNotEmpty(selectByTgId.getUserName())) {
                if (!user.getUserName().equals(selectByTgId.getUserName())) {
                    selectByTgId.setUserName(user.getUserName());
                    tgUserNapper.updateById(selectByTgId);
                    RedisUntils.setValue(Constants.TGUSER + selectByTgId.getTgId(), JSON.toJSONString(selectByTgId));
                }
            }

        }
        if (StringUtils.isNotEmpty(selectByTgId.getFirstName())) {
            if (!selectByTgId.getFirstName().equals(firstNam)) {
                selectByTgId.setFirstName(firstNam);
                tgUserNapper.updateById(selectByTgId);
                RedisUntils.setValue(Constants.TGUSER + selectByTgId.getTgId(), JSON.toJSONString(selectByTgId));
            }
        } else {
            selectByTgId.setFirstName(firstNam);
            tgUserNapper.updateById(selectByTgId);
            RedisUntils.setValue(Constants.TGUSER + selectByTgId.getTgId(), JSON.toJSONString(selectByTgId));
        }
    }
}











